<template>
  <div class="login_end">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <el-avatar :src="require('@/assets/ShoppingCart2.png')"></el-avatar>
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        class="login_form"
        :model="adminForm"
        :rules="lgoinFormRules"
      >
        <el-form-item prop="username">
          <!-- 用户名  -->
          <el-input
            v-model="adminForm.username"
            prefix-icon="iconfont icon-zhanghao"
            placeholder="账号名/邮箱/手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="adminForm.password"
            type="password"
            prefix-icon="iconfont icon-tianchongxing-"
            placeholder="请输入登录密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      adminForm: {
        username: 'admin',
        password: 'admin1',
      },
      lgoinFormRules: {
        //验证用户名是否合法
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    //点击重置按钮，重置表单
 async resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() { 
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const res = await this.$api.rear_end.login(this.adminForm)
        if (res.data.code === 1) {
          this.$message.success('登录成功！')
          // window.sessionStorage.setItem('token', res.data.data)
          localStorage.setItem("token",res.data.data)       

          this.$router.push({
            path:'/rear-end/index',
            
        })
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
  },
}
</script>

<style scoped lang="less">
.login_end {
  background-color: #f1e7df;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);

  /deep/.el-avatar {
    width: 100%;
    height: 100%;
    background-color: #f1e7df;
    img{
      transform: translateX(-35px);
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  //弹性盒模型
  display: flex;
  justify-content: flex-end;
}

/deep/ .el-input__inner {
  text-indent: 12px;
}

/deep/ .iconfont {
  height: 40px;
  width: 40px;
  left: -5px;
  top: 2px;
  position: relative;
  border-radius: 4px;
  display: block;
  background-color: #cccccc;
  color: #ffff;
  font-size: 26px;
}

/deep/.el-input__inner:focus {
  border-color: #c0c4cc;
  /* 修改聚焦时的边框颜色 */
  box-shadow: 0 0 3px #c0c4cc;
  /* 修改聚焦时的阴影效果 */
}
</style>
